package com.neusoft.lohas.modules.ivm.web.business;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.IdGen;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.business.Concession;
import com.neusoft.lohas.modules.ivm.entity.business.ConcessionPhoto;
import com.neusoft.lohas.modules.ivm.entity.business.ConcessionHot;
import com.neusoft.lohas.modules.ivm.service.business.ConcessionService;
/**
 * 商圈最新优惠Controller
 * @author Jackie.Liu
 * @version 2015-01-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/business/concession")
public class ConcessionController extends BaseController {

	@Autowired
	private ConcessionService concessionService;
	
	@ModelAttribute
	public Concession get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return concessionService.get(id);
		}else{
			return new Concession();
		}
	}
	
	@RequiresPermissions("ivm:business:concession:view")
	@RequestMapping(value = {"list", ""})
	public String list(Concession concession, HttpServletRequest request, HttpServletResponse response, Model model) {
		concession.setMerchant(super.merchant);
        Page<Concession> page = concessionService.find(new Page<Concession>(request, response), concession); 
        model.addAttribute("page", page);
		return "modules/ivm/business/concessionList";
	}

	@RequiresPermissions("ivm:business:concession:view")
	@RequestMapping(value = "form")
	public String form(Concession concession, Model model) {
		if (StringUtils.isBlank(concession.getId())) {
			concession.setMerchant(super.merchant);
		}
		model.addAttribute("concession", concession);
		return "modules/ivm/business/concessionForm";
	}

	@RequiresPermissions("ivm:business:concession:edit")
	@RequestMapping(value = "save")
	public String save(Concession concession, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, concession)){
			return form(concession, model);
		}
		if (StringUtils.isBlank(concession.getId())) {
			concession.setId(IdGen.uuid());
			concession.prePersist();
		}
		for (ConcessionPhoto photo : concession.getPhotoList()) {
			if (StringUtils.isBlank(photo.getId())) {
				photo.setConcession(concession);
				photo.setId(IdGen.uuid());
				photo.prePersist();
			}
			for (ConcessionHot hot : photo.getHotList()) {
				if (StringUtils.isBlank(hot.getId())) {
					hot.setConcessionPhoto(photo);
					hot.setId(IdGen.uuid());
					hot.prePersist();
				}
			}
		}
		concessionService.save(concession);
		addMessage(redirectAttributes, "保存最新优惠成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/concession/?repage";
	}
	
	@RequiresPermissions("ivm:business:concession:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		concessionService.delete(id);
		addMessage(redirectAttributes, "删除最新优惠成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/concession/?repage";
	}

}
